package com.job.yangshida.homework0719;

import sun.applet.Main;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Stack;

/**
 * @author 杨士达
 * @version 1.0
 * @since 2022-07-20 12:43:13
 * list里存放了一组数,用栈来排序,降序
 */
public class Sort {
    public static void main(String[] args) {
        Stack<Integer> stackA=new Stack<>();//最终栈
        Stack<Integer> stackB=new Stack<>();//
        List<Integer> list=new ArrayList<>();
        for (int i=0;i<10;i++){
            Random random=new Random();
            list.add(random.nextInt(10));
            //
        }
        System.out.println("对这组数"+list+"进行排序降序");
        System.out.println(list.get(0));
        sort(list,stackA,stackB);

    }


    //yong用栈对表里的数排序
    private static void sort(List<Integer> a,Stack<Integer> s1,Stack<Integer> s2) {
        while (!a.isEmpty()) {//表不空
            if (s1.isEmpty() || a.get(0) >= s1.peek()) {
                s1.push(a.get(0));
                a.remove(0);
            } else {
                //把比要插入数大的元素都挪到s2里
                while (a.get(0) <s1.peek()) {
                    s2.push(s1.pop());
                }
                //charu插入
                s1.push(a.get(0));
                a.remove(0);
                //再挪回来
                while (!s2.isEmpty()) {
                    s1.push(s2.pop());
                }
            }

        }
        if (a.isEmpty()) {
            System.out.println("排完序之后的是");
            while (!s1.isEmpty()) {
                System.out.print(s1.pop() + "\t");//
            }
        }

        //随机生成n个(0---x)整数
   /* private static void generateArray(List<Integer> a,int n) {
        for (int i=0;i<10;i++){
        Random random=new Random();
        list.add(random.nextInt(10));
       //
    }
        System.out.println("对这组数"+a+"进行排序降序");
        System.out.println(list.get(0));
    }*/
    }}
